﻿using FittyWars.BordUtils;
using FittyWars.GraphicUtils;
using FittyWars.MathUtil;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FittyWars
{
    public partial class Display : Form
    {
        #region Members
        private GameManager m_manager;
        private Bitmap m_graphicDisplay;
        #endregion

        #region Properties
        public int DisplayWidth { get; set; }
        public int DisplayHeight { get; set; }
        #endregion

        #region Public methods
        public void UpdateGameDisplay(List<Element> elementList)
        {
            m_graphicDisplay = new Bitmap(DisplayWidth, DisplayHeight);
            using (Graphics g = Graphics.FromImage(m_graphicDisplay))
            {
                g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, DisplayWidth, DisplayHeight));
                //m_graphicDisplay = (Bitmap)global::FittyWars.Properties.Resources.sillyMap.Clone();
                //Graphics g = Graphics.FromImage(m_graphicDisplay);
                foreach (Element elem in elementList)
                {
                    ImageMaker2D.DrawElement(g, elem, DisplayWidth, DisplayHeight);
                }
                MainGraphics.Invalidate();
                // TODO : check without refresh 
                MainGraphics.Refresh();
            }
        }
        #endregion

        public Display(GameManager manager)
        {
            m_manager = manager; 
        }

        public void Initialize()
        {
            InitializeComponent();
            MainGraphics.Width = DisplayWidth = 600;
            MainGraphics.Height = DisplayHeight = 600;
        }

        private void MainGraphics_paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.DrawImage(m_graphicDisplay,new Point(0,0));
        }

        #region Tests
        public void ElzingaHearnTestDisplay(List<Vector> vectors, Circle cc)
        {
            m_graphicDisplay = new Bitmap(600,600);
            Graphics g = Graphics.FromImage(m_graphicDisplay);
            g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, 600, 600));
            g.DrawEllipse(new Pen(Color.Blue),
                Convert.ToInt32(600 * (cc.Center.X - cc.Radious)),
                Convert.ToInt32(600 * (cc.Center.Y - cc.Radious)),
                Convert.ToInt32(1200 * cc.Radious),
                Convert.ToInt32(1200 * cc.Radious)
                );
            SolidBrush sb = new SolidBrush(Color.Red);
            foreach (Vector v in vectors)
            {
                g.FillRectangle(sb,
                    Convert.ToInt32(600 * v.X) - 1,
                    Convert.ToInt32(600 * v.Y) - 1,
                    3, 3);
            }
            g.Dispose();
        }

        public void PolygonTestDisplay(List<Polygon> polyList)
        {
            m_graphicDisplay = new Bitmap(600, 600);
            Graphics g = Graphics.FromImage(m_graphicDisplay);
            g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, 600, 600));
            foreach(Polygon p in polyList)
            {
                Point[] points = p.Vectors.Select(v => new Point(Convert.ToInt32(v.X * 300) + 300, Convert.ToInt32(v.Y * 300) + 300)).ToArray();
                g.DrawPolygon(new Pen(Color.Blue), points);
            }
            g.Dispose();
        }
        #endregion

        #region Event Handlers
        private void MainGraphics_click(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            m_manager.DebugStep();
        }
        #endregion
    }
}
